package uk.ac.lkl.common.associator;



import uk.ac.lkl.common.util.ds.ListHandle;


/**
 * 
 * A wrapper around an Associator, for unknown reasons.
 * Effectivly, this class defines a pairing in the Linking panel.
 * 
 * @author Darren Pearce
 * @version $Revision$
 * @version $Date$
 * 
 */
public class AssociatorHandle {

    private ListHandle leftListHandle;

    // this is a very ugly hack. 
    // this parameter denotes the place this pair has in the linking panel
    private int index = -1;
    
    private Class<?> annotationClass;


    private ListHandle rightListHandle;


    public AssociatorHandle(Class<?> leftListClass, Class<?> annotationClass,
            Class<?> rightListClass) {
        this(new ListHandle(leftListClass), annotationClass, new ListHandle(
                rightListClass));
    }


    public AssociatorHandle(String leftListName, Class<?> leftListClass,
            Class<?> annotationClass, String rightListName,
            Class<?> rightListClass, int uiIndex) {
    	this(leftListName, leftListClass, annotationClass, rightListName, rightListClass);
    	setUIIndex(uiIndex);
    }

    public AssociatorHandle(String leftListName, Class<?> leftListClass,
            Class<?> annotationClass, String rightListName,
            Class<?> rightListClass) {
        this(
             new ListHandle(leftListName, leftListClass),
             annotationClass,
             new ListHandle(rightListName, rightListClass));
    }



    public AssociatorHandle(ListHandle leftListHandle,
            Class<?> annotationClass, ListHandle rightListHandle,
            int index) {
    	this(leftListHandle, annotationClass, rightListHandle);
    	setUIIndex(index);
    }
    
    public AssociatorHandle(ListHandle leftListHandle,
            Class<?> annotationClass, ListHandle rightListHandle) {
        this.leftListHandle = leftListHandle;
        this.annotationClass = annotationClass;
        this.rightListHandle = rightListHandle;
    }


    public ListHandle getLeftListHandle() {
        return leftListHandle;
    }


    public String getLeftListClassName() {
        return getLeftListClass().getName();
    }


    public String getLeftListName() {
        return leftListHandle.getName();
    }


    public Class<?> getLeftListClass() {
        return leftListHandle.getListClass();
    }


    public ListHandle getRightListHandle() {
        return rightListHandle;
    }


    public String getRightListClassName() {
        return getRightListClass().getName();
    }


    public String getRightListName() {
        return rightListHandle.getName();
    }


    public Class<?> getRightListClass() {
        return rightListHandle.getListClass();
    }


    public Class<?> getAnnotationClass() {
        return annotationClass;
    }


    public String getAnnotationClassName() {
        Class<?> annotationClass = getAnnotationClass();
        return annotationClass == null ? null : annotationClass.getName();
    }
    

    public int getUIIndex(){
    	return index;
    }
    
    public void setUIIndex(int index){
    	this.index = index;
    }

    public int hashCode() {
        int hashCode = annotationClass == null ? 0 : annotationClass.hashCode();
        hashCode += leftListHandle.hashCode() + rightListHandle.hashCode();
        return hashCode;
    }


    public boolean equals(Object object) {
        if (!(object instanceof AssociatorHandle))
            return false;

        AssociatorHandle other = (AssociatorHandle) object;

        return this.getLeftListHandle().equals(other.getLeftListHandle())
                && this.annotationClass == other.annotationClass
                && this.getRightListHandle().equals(other.getRightListHandle());
    }

}
